/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package me.send.business.service.mapper;

import java.util.List;
import java.util.Map;

import me.send.business.service.bean.BrokerEvaluate;
import me.send.business.service.bean.UserEvaluateBean;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

/**
 * @author maxmin
 *
 * @date 2015年8月25日 下午4:55:47
 */
public interface BrokerEvaluateMapper {
	/**
	 * 添加对经纪人的评论
	 * @param evaluate
	 * @return
	 * @author maxmin
	 * @date 2015年8月25日下午5:15:21
	 */
	@Insert(value="INSERT INTO broker_evaluate (point,content,orderid,userid,createtime,updatetime) "
			+ "VALUES (#{point},#{content},#{orderid},#{userid},#{createtime},#{updatetime})")
	public int addUserEvaluate(BrokerEvaluate evaluate);
	
	@Select(value = "SELECT ue.content,ue.createtime,u.nickname,u.headerpath,ue.point,ue.evaluate_id,o.orderid,ue.userid,u.sex,ue.createtime "
			+ "FROM broker_evaluate ue LEFT JOIN orders o ON o.orderid = ue.orderid "
			+ "LEFT JOIN `user` u ON o.userid = u.userid WHERE o.brokerid=#{brokerid}")
	@Results({
		@Result(column="evaluate_id",property="evaluateid")
	})
	public List<Map<String, Object>> getEvaluates(int brokerid); 
	
	@Select(value="SELECT p.telephone,p.popid brokerid,p.username,p.headerpath,p.`level`,"
			+ "( SELECT COUNT(1) FROM orders o LEFT JOIN orderbroker obk ON obk.order_id = o.orderid WHERE obk.broker_id = p.popid AND o.`status` >= 8 ) ordercount,"
			+ "( SELECT IF ( COUNT(1) > 0, SUM(be.point) / COUNT(be.point), 0 ) FROM broker_evaluate be LEFT JOIN orderbroker obk ON be.orderid = obk.order_id WHERE obk.broker_id = p.popid ) stars,"
			+ "( SELECT IF (COUNT(1) > 0, 1, 0) FROM collectionuser cu LEFT JOIN popuser pu ON pu.popid = cu.popuserid WHERE cu.userid = #{userid} AND pu.popid = p.popid ) iscollection "
			+ "FROM popuser p LEFT JOIN orderbroker ob ON ob.broker_id = p.popid WHERE ob.order_id=#{orderid} AND ischecked = 1")
	public Map<String, Object> toEvaluate(Map<String, Object> params);
	
	@Select(value="SELECT be.content,be.createtime,be.evaluate_id evaluateid,be.point,p.username nickname FROM broker_evaluate be "
			+ "LEFT JOIN `user` u ON be.userid = u.userid "
			+ "LEFT JOIN orders o ON o.orderid = be.orderid "
			+ "LEFT JOIN orderbroker ob ON ob.order_id = o.orderid "
			+ "LEFT JOIN popuser p ON p.popid=ob.broker_id "
			+ "WHERE u.userid = #{userid} AND ob.ischecked=1 AND IF(#{startid}>0,be.evaluate_id<#{startid},1=1) ORDER BY be.evaluate_id DESC LIMIT #{size}")
	public List<UserEvaluateBean> myEvaluates(Map<String, Object> params);
}
